package shen.yue.example.one.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import shen.yue.example.one.R;
import shen.yue.example.one.constants.StringConstants;

/**
 * Created by jiangzhuolei on 2017/8/2.
 */

public class MainAdapter extends BaseAdapter {

    private Context mContext;
    private String[] names;
    private Class[] activitys;

    public MainAdapter(Context mContext){
        this.mContext = mContext;
        names = StringConstants.names;
        activitys = StringConstants.activitys;
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            view = View.inflate(mContext, R.layout.item_main,null);
            holder = new ViewHolder();
            holder.tv_main_name = (TextView) view.findViewById(R.id.tv_main_name);
            holder.btn_main_onclick = (Button) view.findViewById(R.id.btn_main_onclick);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        holder.tv_main_name.setText(names[i]);
        holder.btn_main_onclick.setText(names[i]);
        holder.btn_main_onclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,activitys[i]);
                mContext.startActivity(intent);
            }
        });

        return view;
    }


    static class ViewHolder{
        public TextView tv_main_name;
        public Button btn_main_onclick;
    }
}
